#include "BlockTank2.h"


BlockTank2::BlockTank2(int X, int Y)
{
	m_Sprite = Source::getInstance()->getSprite(IDImage::IMG_BlockTank2);
	m_Position.x = (float)X;
	m_Position.y = (float)Y;

	m_Index = 0;
	m_Blood = 7.0f;
	m_TimeAnimation = 0.8f;
	m_Gametime = new GameTime();

	loadPositionGunAtSprite("Block Tank//PositionGunBlockTank2.txt");
	m_ToolBullet = new Toolbullet(3, 2.0f);
	m_ToolBullet->setGun(IDGun::Enemy_Gun);
}
float BlockTank2::GetTanX(int x)
{
	switch (x)
	{
	case 25:
		return 0.466f;
		break;
	case 50:
		return 1.192f;
		break;
	}
}
void BlockTank2::UpdateAnimation(float X, float Y)
{
	m_Gametime->setTime();
	if (m_Gametime->getElapsedTimePerSecond() >= m_TimeAnimation)
	{
		if (abs(m_Vpx - m_Position.x) >= SCREEN_WIDTH) return;

		m_PositionHero_X = X;
		m_PositionHero_Y = Y;

		float x = X - m_Position.x;
		float y = Y - m_Position.y;
		m_Flip = SpriteEffect::Flip;
		TanX = abs(y / x);
		if (TanX <= GetTanX(25))
		{
			m_Index = 2;
			m_shootDirection = ShootDirection::_0_SHOOT;
		}
		else if (TanX > GetTanX(25) && TanX <= GetTanX(50))
		{
			m_Index = 3;
			m_shootDirection = ShootDirection::_30_SHOOT;
		}
		else if (TanX > GetTanX(50))
		{
			m_Index = 4;
			m_shootDirection = ShootDirection::_60_SHOOT;
		}
	}
}
void BlockTank2::Update()
{
	if (m_Gametime->getElapsedTimePerSecond() >= m_TimeAnimation)
	{
		m_Gametime->Update();

		if (m_PositionHero_X < m_Position.x && m_PositionHero_Y > m_Position.y)
		{
			for (int i = 0; i < m_positionShootAtSprite.size(); i++)
			{
				if (m_Index == m_positionShootAtSprite[i].m_Index)
				{
					m_ToolBullet->InitBullet(m_Position.x, m_Position.y, m_Sprite->getWidth(), m_Sprite->getHeight(), m_positionShootAtSprite[i].m_X, m_positionShootAtSprite[i].m_Y, m_Flip, m_shootDirection, TypeMove::Move_OnTheLand);
					m_ToolBullet->Shoot();
				}
			}
		}
	}

	m_ToolBullet->Update();
}
void BlockTank2::Draw(LPD3DXSPRITE SpriteHandler)
{
	m_Sprite->setIndex(m_Index);
	Character::Draw(SpriteHandler);
}


BlockTank2::~BlockTank2()
{
}
